<template>
  <div>
    <div>【大区异常处理】</div>
    <div ref="ringRef" class="w-full h-full"></div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, watch } from 'vue'
import * as echarts from 'echarts'

const props = defineProps({
  data: {
    type: Object,
    required: true
  }
})

const ringRef = ref(null)
let myChart: any = null
onMounted(() => {
  myChart = echarts.init(ringRef.value)
  renderChart()
})

/**
 * 双环形图绘制原理：
 * 1. 环形图通过饼图绘制。内外边距的距离减小，即为环形。环形中心点需要不断改变，否则会重叠
 * 2. 环形图绘制分为 上层和底层 两部分。上层作为绘制进度，底层作为背景图
 * 3. 依据 getSeriesData 生成对应的 上层和底层 series 数据，进行渲染
 */
const getSeriesData = () => {
  const series = [] as any

  props.data.abnormals.forEach((item: any, index: number) => {
    // 上层
    series.push({
      name: item.name,
      type: 'pie',
      clockwise: false,
      animation: false,
      radius: [73 - index * 15 + '%', 68 - index * 15 + '%'],
      center: ['55%', '55%'],
      label: {
        show: false,
      },
      data: [
        {value: item.value, name: item.name},
        {
          value: 1000,
          name: '',
          itemStyle: {
            color: 'transparent',
            borderWidth: 0,
          },
          tooltip: { show: false},
          animation: false,
        }
      ]
    })

    // 底层
    series.push({
      name: item.name,
      type: 'pie',
      // 不相应事件
      slient: false,
      z: 1,
      clockwise: false,
      animation: false,
      radius: [73 - index * 15 + '%', 68 - index * 15 + '%'],
      center: ['55%', '55%'],
      label: {
        show: false,
      },
      data: [
        {
          value: 7.5, 
          itemStyle: {
            color: 'rgb(3, 31, 62)',
            borderWidth: 0
          },
          tooltip: {
            show: false
          },
          animation: false,
        },
        {
          value: 2.5, 
          name: '',
          itemStyle: {
            color: 'transparent',
            borderWidth: 0
          },
          tooltip: {
            show: false
          },
          animation: false,
        }
      ]
    })
  });
    return series

}

const renderChart = () => {
  let option = {
    // 图例配置
    legend: {
      show: true,
      icon: 'circle',
      top: '14%',
      left: '60%',
      data: props.data.abnormals.map((item: any) => item.name),
      width: -5,
      itemWith: 10,
      itemHeight: 10,
      itemGap: 6,
      textStyle: {
        fintSize: 14,
        color: 'rgba(255, 255, 255, 0.8)',
        linHeight: 5
      }
    },
    // 提示文字
    tooltip: {
      show: true,
      tigger: 'item',
      formatter: '{a}<br>{b}:{c}({d}%)'
    },
    // y轴
    yAxis: [{ 
      type: 'category',
      inverse: true,
      axisLine: {
        show: false
      }
    }],
    xAxis: [{
      show: false
    }],
    // x轴
    series: getSeriesData()

  }
  myChart.setOption(option)
}

watch(() => props.data, () => {
  renderChart()
})
</script>

<style scoped>
</style>
